package yixaun.tim.service.impl;

import org.springframework.util.CollectionUtils;
import yixaun.tim.bean.onLineStates.OnLineVO;
import yixaun.tim.common.TimException;
import yixaun.tim.service.TimOnlineStatusService;
import yixaun.tim.service.TimService;
import yixaun.tim.util.JsonUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author pengshiqing
 * @Date: 2019/6/24
 * @Description:
 */
public class TimOnlineStatusServiceImpl implements TimOnlineStatusService {

    private TimService timService;

    public TimOnlineStatusServiceImpl(TimService timService) {
        this.timService = timService;
    }


    /**
     * 获取用户在线状态
     * @param accounts
     */
    @Override
    public OnLineVO queryState(List<String> accounts) throws TimException {
        if (CollectionUtils.isEmpty(accounts)) {
            throw new TimException(0,"accounts 不能为空");
        }

        if (accounts.size() > 500) {
            throw new TimException(0, "批量发消息目标帐号超过500，请减少 To_Account 中目标帐号数量");
        }

        String api = "v4/openim/querystate";

        Map<String, List<String>> queryStateBody = new HashMap<>();
        queryStateBody.put("To_Account", accounts);

        String jsonResult = timService.post(api, queryStateBody);
        return JsonUtils.fromJson(jsonResult, OnLineVO.class);
    }
}
